package org.groupg.practice;

import org.mapdb.*;
import java.util.Map;

public class SimpleDocumentStore {
    public static void main(String[] args) {
        DB db = DBMaker.fileDB("documents.db")
                .transactionEnable()
                .make();

        // 创建文档存储
        Map<String, Map<String, Object>> documentStore = db
                .hashMap("documents", Serializer.STRING, Serializer.JAVA)
                .createOrOpen();

        // 创建文档1
        Map<String, Object> userDoc = Map.of(
            "id", "user-123",
            "name", "Alice",
            "age", 30,
            "email", "alice@example.com"
        );

        // 创建文档2
        Map<String, Object> productDoc = Map.of(
            "id", "prod-456",
            "name", "Laptop",
            "price", 1299.99,
            "inStock", true
        );

        // 存储文档
        documentStore.put(userDoc.get("id").toString(), userDoc);
        documentStore.put(productDoc.get("id").toString(), productDoc);

        // 提交事务
        db.commit();

        // 检索文档
        System.out.println("User Document:");
        Map<String, Object> retrievedUser = documentStore.get("user-123");
        retrievedUser.forEach((key, value) -> System.out.println(key + ": " + value));

        System.out.println("\nProduct Document:");
        Map<String, Object> retrievedProduct = documentStore.get("prod-456");
        retrievedProduct.forEach((key, value) -> System.out.println(key + ": " + value));

        db.close();
    }
}